package com.ruike.alisurface.ui.payPage;

import android.os.Bundle;
import android.widget.TextView;

import com.ruike.alisurface.https.ShopApiHttp;
import com.ruike.alisurface.R;
import com.ruike.alisurface.base.BaseAty;
import com.ruike.alisurface.printer.OrderTicketBean;
import com.ruike.alisurface.printer.PrinterOperateUtils;
import com.ruike.alisurface.ui.ShopMain_Aty;
import com.ruike.alisurface.utils.GsonUtils;
import com.ruike.alisurface.utils.ShareKey;
import com.ruike.alisurface.views.LoadingView;
import com.voodoo.lib_http.ApiListener;
import com.voodoo.lib_logcat.L;
import com.voodoo.lib_utils.CountDownTimer;
import com.voodoo.lib_utils.SharedUtils;

import org.json.JSONObject;

import butterknife.BindView;

/**
 * 出货结果完成页
 */
public class PayShipmentResult_Aty extends BaseAty {
    @BindView(R.id.selectPay_countDown_tv)
    TextView selectPayCountDownTv;
    @BindView(R.id.order_outGoodsResult_loadv)
    LoadingView orderOutGoodsResultLoadv;
    @BindView(R.id.order_money)
    TextView orderMoneyTv;
    @BindView(R.id.actual_money)
    TextView actualMoneyTv;
    @BindView(R.id.discount)
    TextView discountTv;
    @BindView(R.id.order_outGoodsResult_tv)
    TextView outGoodsResultTv;

    @Override
    protected int getLayoutResId() {
        return R.layout.app_payshipment_result;
    }

    @Override
    protected void initViews() {
        initTitleBar(false);
        startCountdownTimer(10, selectPayCountDownTv, ShopMain_Aty.class);
    }

    boolean isSuccess;
    String order_id;

    @Override
    protected void initData() {
        PrinterOperateUtils.getInstance().connectUsbPrinter(this);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            order_id = bundle.getString("order_id");
            double order_money = bundle.getDouble("order_money");
            double actual_money = bundle.getDouble("actual_money");
            isSuccess = bundle.getBoolean("isSuccess");
            bundle.getSerializable("slotList");
            outGoodsResultTv.setText(isSuccess ? "出货成功" : "出货失败");
            orderMoneyTv.setText(new StringBuffer("订单金额: ￥").append(order_money));
            actualMoneyTv.setText(new StringBuffer("消费金额: ￥").append(actual_money));
//            discountTv.setText(new StringBuffer(" ￥").append(discount));
            new CountDownTimer(1000, 1000) {

                @Override
                public void onTick(long millisUntilFinished) {
                }

                @Override
                public void onFinish() {
                    orderOutGoodsResultLoadv.setStatus(isSuccess ? LoadingView.STATUS_SUCCESS : LoadingView.STATUS_FAIL);
                }
            }.start();
            boolean isprinter = SharedUtils.getInstance().getBoolean(ShareKey.IS_PRINTER,true);
            if (isprinter) {
                ShopApiHttp.get_print_ticket_date(order_id, new ApiListener() {
                    @Override
                    public void onRequestStart(String requestUrl, int requestId) {

                    }

                    @Override
                    public void onRequestComplete(String requestUrl, int requestId) {

                    }

                    @Override
                    public void onSuccess(String requestUrl, JSONObject requestJson, int requestId) {
                        OrderTicketBean orderTicketBean = GsonUtils.fromJson(requestJson.toString(), OrderTicketBean.class);
                        L.e("请求小票成功==" + requestJson.toString());
                        // 打印属于耗时操作，在异步线程完成
                        new Thread() {
                            @Override
                            public void run() {
                                if (orderTicketBean != null && orderTicketBean.getData() != null) {
                                    PrinterOperateUtils.getInstance().printOrder(getResources(), orderTicketBean.getData());
                                }
                            }
                        }.start();
                    }

                    @Override
                    public void onError(String requestUrl, String errorMsg, String resultData, int requestId) {
                        L.e("请求小票异常==" + errorMsg);
                    }
                });

            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        PrinterOperateUtils.getInstance().unConnectUsbPrinter();

    }
}
